﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HsTec.DigitalPlatform.DataAccess
{
    public class MenuInfoRepository
    {
        DigitalPlatformEntities db = new DigitalPlatformEntities();

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="menuinfo"></param>
        public void Add(MenuInfo menuinfo)
        {
            db.AddToMenuInfos(menuinfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="menuinfo"></param>
        public void Delete(MenuInfo menuinfo)
        {
            db.DeleteObject(menuinfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            db.SaveChanges();
        }

        /// <summary>
        /// 根据ID查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MenuInfo GetMenuInfoByID(int id)
        {
            return db.MenuInfos.Single<MenuInfo>(d => d.menuid == id);
        }

        /// <summary>
        /// 返回所有实体
        /// </summary>
        /// <returns></returns>
        public IQueryable<MenuInfo> FindALLMenuInfo()
        {
            return db.MenuInfos;
        }

        /// <summary>
        /// 根据父节点查找相应子节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<MenuInfo> FindChildMenuInfoByParentID(int id)
        {
            return db.MenuInfos.Where<MenuInfo>(d => d.parentid == id);
        }

        public MenuInfo GetRoot()
        {
            return db.MenuInfos.FirstOrDefault<MenuInfo>(d => d.parentid == 0);
        }
    }
}
